extends Node

var is_socketConnected = false
var socket = WebSocketPeer.new()

func init_to_connect():
	var result = socket.connect_to_url("ws://"+ Config.SERVER_ADDRESS +":8082")
	if result == OK : 
		print("webSocket 连接成功")
		is_socketConnected = true
		set_process(true)
	
func _process(_delta):
	socket.poll()
	var state = socket.get_ready_state()
	if state == WebSocketPeer.STATE_OPEN:
		while socket.get_available_packet_count():
			var json = socket.get_packet().get_string_from_utf8()
			on_receive(JSON.parse_string(json))
	elif state == WebSocketPeer.STATE_CLOSING:
		print("WebSocket 正在关闭")
	elif state == WebSocketPeer.STATE_CLOSED:
		set_process(false)
		if is_socketConnected:
			print("WebSocket 已关闭")
			ObjectManager.toast.showToast("网络已断开")
		

# 给服务器发送数据
# isGame:是否是游戏内的消息
# data:要处理的数据
# method:方法 
#	userBind:username绑定client
#	chat:聊天室发消息
func sendObj(isGame:bool,method:String,data:Dictionary):
	var obj:Dictionary = {"isGame":isGame,"method":method,"data":data}
	var json:String = JSON.stringify(obj)
	var error = socket.send_text(json)
	if error == OK :
		print("sendObj isGame = ",isGame,", method  = ",method,", data = ",data)

# 接收服务端数据
func on_receive(dic:Dictionary):
	if dic.isLog == null || dic.isLog:
		print("on_receive:",dic)
	if ObjectManager.is_inGame && dic.isGame:
		match dic.method:
			"init_players":
				ObjectManager.gameManager.init_players(dic.data)
			"init_map":
				ObjectManager.gameManager.init_map(dic.data)
			"gameStart":
				pass
			"switchTurnPlay":
				ObjectManager.gameManager.switchTurnPlay(dic.data)
			"updateTurnTime":
				ObjectManager.gameManager.ui.updateTurnTime(dic.data)
			"update_point":
				ObjectManager.gameManager.handlerManager.pointHandler.update_point(dic.data.point)
			"update_small":
				ObjectManager.gameManager.handlerManager.smallHandler.update_small(dic.data.small)
			"update_big":
				ObjectManager.gameManager.handlerManager.bigHandler.update_big(dic.data.big)
			"small_die":
				ObjectManager.gameManager.handlerManager.smallHandler.small_die(dic.data)
			"born_big":
				ObjectManager.gameManager.handlerManager.bigHandler.born_big(dic.data.big)
			"big_move":
				ObjectManager.gameManager.handlerManager.bigHandler.big_move(dic.data)
			"big_attack":
				ObjectManager.gameManager.handlerManager.bigHandler.big_attack(dic.data)
			"evolve":
				ObjectManager.gameManager.handlerManager.bigHandler.evolve(dic.data)
			"big_die":
				ObjectManager.gameManager.handlerManager.bigHandler.big_die(dic.data)
			"big_possess":
				ObjectManager.gameManager.handlerManager.bigHandler.big_possess(dic.data)
			"幽灵_游荡":
				ObjectManager.gameManager.handlerManager.handler_幽灵.游荡(dic.data)
			"恶灵_灵狩":
				ObjectManager.gameManager.handlerManager.handler_恶灵.灵狩(dic.data)
			"恶灵_附身":
				ObjectManager.gameManager.handlerManager.handler_恶灵.附身(dic.data)
			"showToast":
				ObjectManager.toast.showToast(dic.data.message)
			"gameOver":
				ObjectManager.gameManager.gameOver(dic.data)
			"顽石_倒计时":
				ObjectManager.gameManager.顽石_倒计时(dic.data)
			"大炮_bombing":
				ObjectManager.gameManager.handlerManager.handler_炮台.大炮_bombing(dic.data)
			"狙炮_aim":
				ObjectManager.gameManager.handlerManager.handler_狙炮.狙炮_aim(dic.data)
			"地灵_抹平":
				ObjectManager.gameManager.handlerManager.handler_地灵.抹平(dic.data)
			"地灵_占据":
				ObjectManager.gameManager.handlerManager.handler_地灵.占据(dic.data)
			"灵阵_布阵":
				ObjectManager.gameManager.handlerManager.handler_灵阵.布阵(dic.data)
			"拳师_怒威神拳":
				ObjectManager.gameManager.handlerManager.handler_拳师.怒威神拳(dic.data)
			"拳师_爆裂火拳":
				ObjectManager.gameManager.handlerManager.handler_拳师.爆裂火拳(dic.data)
			"update_拳师_mark":
				ObjectManager.gameManager.handlerManager.handler_拳师.update_拳师_mark(dic.data)
			"remove_拳师_mark":
				ObjectManager.gameManager.handlerManager.handler_拳师.remove_拳师_mark(dic.data)
			"天谕_update":
				ObjectManager.gameManager.handlerManager.handler_天谕.天谕_update(dic.data)
			"野马_践踏":
				ObjectManager.gameManager.handlerManager.handler_野马.践踏(dic.data)
			"play_effect":
				ObjectManager.gameManager.play_effect(dic.data)
			"play_sound":
				ObjectManager.gameManager.play_sound(dic.data)
			"test":
				print("未知")
	elif !ObjectManager.is_inGame:
		match dic.method:
			"gameReady":
				ObjectManager.main.jump_to_game()
			"chat":
				ObjectManager.chatRoom.add_label(dic)
			"updateOnline":
				ObjectManager.chatRoom.update_online(dic.data.number)
			"initSpendData":
				SpendData.initData(dic.data)
			"matchFail":
				ObjectManager.toast.showToast(dic.data.message)
				ObjectManager.personCenter._on_取消匹配_pressed()
			"watchFail":
				ObjectManager.toast.showToast(dic.data.message)
			"test":
				print("未知")
